<!DOCTYPE html>
<html>
    <head>
        <title>Feedbin Receipt</title>
        <%= stylesheet_link_tag "application", media: 'all' %>
        <style type="text/css" media="screen">
            body {
                padding: 20px;
            }
            td,th,table {
                border-color: #000 !important;
            }
        </style>
    </head>
    <body>
        <h2 class="no-margin">Receipt</h2>
        <p>
            <small>Invoice: <%= @billing_event.event_object["invoice"] || @billing_event.event_object["id"] %></small>
            <br />
            <small><%= Time.at(@billing_event.event_object["created"]).to_formatted_s(:date) %></small>
        </p>
        <div class="customer-info">
            <strong>Account</strong>
            <% unless @user.receipt_info.blank? %>
                <br />
                <%= strip_tags(@user.receipt_info).gsub("\n", "<br />").html_safe %>
            <% end %>
            <br />
            <%= @user.email %>
        </div>
        <div class="company-info">
            <strong>Feedbin, Inc.</strong><br />
            <%= business_address %>
        </div>
        <hr class="space" />
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th width="50%">Description</th>
                    <th width="50%">Price</th>
                </tr>
            </thead>

            <% if @billing_event.invoice_items.present? %>
                <% @billing_event.invoice_items["data"].each do |invoice_item| %>
                    <tr>
                        <td>
                            <% if invoice_item["plan"] %>
                                Subscription to <%= invoice_item["plan"]["id"] %>
                            <% else %>
                                <%= invoice_item["description"] %>
                            <% end %>
                        </td>
                        <td align="right"><%= invoice_item["currency"].upcase %> <%= number_to_currency(invoice_item["amount"].to_f / 100) %></td>
                    </tr>
                <% end %>
                <% if @billing_event.invoice["starting_balance"] != 0 %>
                    <tr>
                        <td>Applied balance</td>
                        <td align="right"><%= @billing_event.invoice["currency"].upcase %> <%= number_to_currency(@billing_event.invoice["starting_balance"].to_f / 100) %></td>
                    </tr>
                <% end %>
                <tr>
                    <th align="left"><strong>Paid</strong></th>
                    <th align="right"><%= @billing_event.event_object["currency"].upcase %> <strong><%= number_to_currency(@billing_event.event_object["amount"].to_f / 100) %></strong></th>
                </tr>
            <% else %>
            <tr>
                <td>
                    <%= @billing_event.event_object.safe_dig("calculated_statement_descriptor") %> <%= @billing_event.event_object.safe_dig("description") %>
                </td>
                <td align="right"><%= @billing_event.event_object.safe_dig("currency").upcase %> <%= number_to_currency(@billing_event.event_object.safe_dig("amount").to_f / 100) %></td>
            </tr>
            <% end %>
        </table>
        <p><strong>Thank You!</strong></p>
    </body>
</html>
